package com.oa.ylms.apps.dao.move.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.move.IPersonMoveInfoDao;
import com.oa.ylms.apps.entity.move.PersonMoveInfoValue;

public class PersonMoveInfoDaoImpl extends HibernateDaoSupport implements
		IPersonMoveInfoDao {
	List<PersonMoveInfoValue> list;

	@Override
	public int addPersonMoveInfo(PersonMoveInfoValue personMoveInfo)
			throws Exception {
		if(personMoveInfo != null){
			this.getHibernateTemplate().save(personMoveInfo);
			return personMoveInfo.getMoveId();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final PersonMoveInfoValue personMoveInfo, int rows)
			throws Exception {
		int pageNumbers = 0;
		String hqlStr = "FROM PersonMoveInfoValue pmiv WHERE 1=1";
		if(personMoveInfo.getStaffInfo() != null){
			hqlStr += " AND pmiv.staffInfo = ?";
		}
		if(personMoveInfo.getStates() != null){
			hqlStr += " AND pmiv.states = ?";
		}
		hqlStr += " AND pmiv.states <> ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(personMoveInfo.getStaffInfo() != null){
					query.setEntity(++index, personMoveInfo.getStaffInfo());
				}
				if(personMoveInfo.getStates() != null){
					query.setInteger(++index, personMoveInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		int numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	@Override
	public PersonMoveInfoValue queryByMoveId(PersonMoveInfoValue personMoveInfo)
			throws Exception {
		if(personMoveInfo != null){
			return (PersonMoveInfoValue) this.getHibernateTemplate().get(PersonMoveInfoValue.class, personMoveInfo.getMoveId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PersonMoveInfoValue> queryPersonMoveInfoWithPage(
			final PersonMoveInfoValue personMoveInfo, final int rows, final int pageNo)
			throws Exception {
		String hqlStr = "FROM PersonMoveInfoValue pmiv WHERE 1=1";
		if(personMoveInfo.getStaffInfo() != null){
			hqlStr += " AND pmiv.staffInfo = ?";
		}
		if(personMoveInfo.getStates() != null){
			hqlStr += " AND pmiv.states = ?";
		}
		hqlStr += " AND pmiv.states <> ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(personMoveInfo.getStaffInfo() != null){
					query.setEntity(++index, personMoveInfo.getStaffInfo());
				}
				if(personMoveInfo.getStates() != null){
					query.setInteger(++index, personMoveInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PersonMoveInfoValue> queryPersonMoveInfoByCheckStaffId(
			com.oa.ylms.apps.entity.move.PersonMoveCheckValue personMoveCheck) throws Exception {
		String hql = "SELECT pmiv FROM PersonMoveInfoValue pmiv, PersonMoveCheckValue pmcv WHERE pmiv.moveId = pmcv.personMoveInfo AND pmcv.staffInfo = ?";
		return this.getHibernateTemplate().find(hql, personMoveCheck.getStaffInfo());
	}
	
	
	public List<PersonMoveInfoValue> getList() {
		return list;
	}

	public void setList(List<PersonMoveInfoValue> list) {
		this.list = list;
	}

	@Override
	public void modifyPersonMoveInfo(PersonMoveInfoValue personMoveInfo) {
		this.getHibernateTemplate().update(personMoveInfo);
		
	}

}
